<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "sys_role".
 *
 * @property int $id ID
 * @property string $name 角色名称
 * @property string $addtime 添加时间
 */
class SysRole extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_role';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'addtime'], 'required'],
            [['addtime'], 'integer'],
            [['name'], 'string', 'max' => 30],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '角色名称',
            'addtime' => '添加时间',
        ];
    }
    
    //获取列表信息 带分页数据
    public static function getRoleList($condition,$page_no){
        //获取符合条件的商品的个数
        $perpage = 15;
        $sql_condition_arr = [];
        array_push($sql_condition_arr,'1=1');
        if($condition['name'] != 0){
            array_push($sql_condition_arr,'name='.$condition['name']);
        }
        
        $sql_condition_string = implode(' and ', $sql_condition_arr);
        $data['total'] = self::getCountByCondition($sql_condition_string);
        $begin = $perpage*($page_no-1);
        $sourcedata = self::getGoodsByPage($sql_condition_string, $begin, $perpage);
        $data['pagedata'] = $sourcedata;
        $data['pagenums'] = ceil($data['total']/$perpage);
        $data['page_no'] = $page_no;
        $data['perpage'] = $perpage;
        return $data;
    }
    
    public static function getCountByCondition($sql_condition_string){
        $sql = "select count(id) total from sys_role where $sql_condition_string";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryOne();
        return $model['total'];
    }
    
    //按页获取数据
    public static function getGoodsByPage($sql_condition_string,$begin,$perpage){
        $sql = "select id,name,addtime from sys_role where $sql_condition_string  limit $begin,$perpage";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
}
